package com.yougou.ui.order;

import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yougou.R;
import com.yougou.base.BaseActivity;
import com.yougou.data.model.Address;
import com.yougou.data.model.Goods;
import com.yougou.data.model.Order;
import com.yougou.ui.address.AddressListActivity;
import com.yougou.utils.AddressManager;
import com.yougou.utils.OrderManager;
import com.yougou.utils.ToastUtils;
import com.yougou.utils.UserManager;

import java.util.Locale;

public class OrderConfirmActivity extends BaseActivity {
    
    public static final String EXTRA_GOODS_ID = "goods_id";
    public static final String EXTRA_GOODS_NAME = "goods_name";
    public static final String EXTRA_GOODS_PRICE = "goods_price";
    public static final String EXTRA_GOODS_IMAGE = "goods_image";
    public static final String EXTRA_QUANTITY = "quantity";
    
    private ImageView mBtnBack;
    private TextView mTvUsername;
    private TextView mTvPhone;
    private TextView mTvAddress;
    private ImageView mIvGoodsImage;
    private TextView mTvGoodsName;
    private TextView mTvGoodsPrice;
    private TextView mTvQuantity;
    private TextView mTvTotalPrice;
    private Button mBtnSubmitOrder;
    
    private int mGoodsId;
    private String mGoodsName;
    private double mGoodsPrice;
    private String mGoodsImage;
    private int mQuantity = 1;
    
    private Address mCurrentAddress;
    private static final int REQUEST_SELECT_ADDRESS = 100;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_confirm);
        
        // 获取商品信息
        mGoodsId = getIntent().getIntExtra(EXTRA_GOODS_ID, 0);
        mGoodsName = getIntent().getStringExtra(EXTRA_GOODS_NAME);
        mGoodsPrice = getIntent().getDoubleExtra(EXTRA_GOODS_PRICE, 0);
        mGoodsImage = getIntent().getStringExtra(EXTRA_GOODS_IMAGE);
        mQuantity = getIntent().getIntExtra(EXTRA_QUANTITY, 1);
        
        initView();
        initData();
    }
    
    @Override
    protected void initView() {
        mBtnBack = findViewById(R.id.iv_back);
        mTvUsername = findViewById(R.id.tv_username);
        mTvPhone = findViewById(R.id.tv_phone);
        mTvAddress = findViewById(R.id.tv_address);
        mIvGoodsImage = findViewById(R.id.iv_goods_image);
        mTvGoodsName = findViewById(R.id.tv_goods_name);
        mTvGoodsPrice = findViewById(R.id.tv_goods_price);
        mTvQuantity = findViewById(R.id.tv_quantity);
        mTvTotalPrice = findViewById(R.id.tv_total_price);
        mBtnSubmitOrder = findViewById(R.id.btn_submit_order);
        
        // 返回按钮
        mBtnBack.setOnClickListener(v -> finish());
        
        // 点击地址区域，跳转到地址选择
        findViewById(R.id.card_address).setOnClickListener(v -> selectAddress());
        
        // 提交订单
        mBtnSubmitOrder.setOnClickListener(v -> submitOrder());
    }
    
    @Override
    protected void initData() {
        // 加载收货地址
        loadAddress();
        
        // 显示商品信息
        mTvGoodsName.setText(mGoodsName);
        mTvGoodsPrice.setText(String.format(Locale.CHINA, "¥%.2f", mGoodsPrice));
        mTvQuantity.setText("x" + mQuantity);
        
        // 加载商品图片
        if (mGoodsImage != null && !mGoodsImage.isEmpty()) {
            Glide.with(this)
                    .load(mGoodsImage)
                    .placeholder(R.drawable.placeholder_image)
                    .error(R.drawable.placeholder_image)
                    .into(mIvGoodsImage);
        }
        
        // 计算总价
        double totalPrice = mGoodsPrice * mQuantity;
        mTvTotalPrice.setText(String.format(Locale.CHINA, "¥%.2f", totalPrice));
    }
    
    
    private void loadAddress() {
        mCurrentAddress = AddressManager.getDefaultAddress();
        if (mCurrentAddress != null) {
            mTvUsername.setText(mCurrentAddress.getName());
            mTvPhone.setText(mCurrentAddress.getPhone());
            mTvAddress.setText(mCurrentAddress.getFullAddress());
            mBtnSubmitOrder.setEnabled(true);
        } else {
            // 没有地址
            mTvUsername.setText("请添加收货地址");
            mTvPhone.setText("点击此区域添加");
            mTvAddress.setText("暂无收货地址，请先添加");
            mBtnSubmitOrder.setEnabled(false);
        }
    }
    
    
    private void selectAddress() {
        android.content.Intent intent = new android.content.Intent(this, AddressListActivity.class);
        intent.putExtra(AddressListActivity.EXTRA_SELECT_MODE, true);
        startActivityForResult(intent, REQUEST_SELECT_ADDRESS);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, android.content.Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_SELECT_ADDRESS && resultCode == RESULT_OK && data != null) {
            Address address = (Address) data.getSerializableExtra(AddressListActivity.EXTRA_SELECTED_ADDRESS);
            if (address != null) {
                mCurrentAddress = address;
                mTvUsername.setText(address.getName());
                mTvPhone.setText(address.getPhone());
                mTvAddress.setText(address.getFullAddress());
                mBtnSubmitOrder.setEnabled(true);
            }
        }
    }
    
    
    private void submitOrder() {
        // 检查地址
        if (mCurrentAddress == null) {
            ToastUtils.show(this, "请先选择收货地址");
            selectAddress();
            return;
        }
        
        // 创建并保存订单
        Order order = OrderManager.createOrder(
                mGoodsName, 
                mGoodsPrice, 
                mQuantity, 
                mGoodsImage,
                mCurrentAddress.getName(),
                mCurrentAddress.getPhone(),
                mCurrentAddress.getFullAddress()
        );
        
        ToastUtils.show(this, "订单提交成功！订单号：" + order.getOrderNumber());
        
        // 返回上一页
        finish();
    }
}

